<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 9:54
 */

namespace app\admin\controller;


use app\admin\model\Wxsetting;

class WxsettingController extends BaseController
{

    //小程序appid，appsecret，mchid，key配置信息，更新和新增保存到缓存
    public function index()
    {
        //如果是新增或者更新操作
        if($this->request->isPost()){
            $data = $this->request->post(); //获取传递数据
            //验证
            $check = $this->validate($data,[
                'appid' => 'require',
                'appsecret' => 'require'
            ]);
            if(!$check){
                return ajaxMsg(1,$check);
            }
            //获取默认第一条
            $wxsetting = Wxsetting::get(1);
            //不存在则为新增操作
            if(!$wxsetting){
                $data = Wxsetting::create($data);
                //保存到缓存
                $this->getSetting('wxconfig','Wxsetting',$data);
                return ajaxMsg(1,'更新成功');
            }else{
                $data['id'] = $wxsetting->id;
                $wx = new Wxsetting();
                //或者为更新操作，强制更新参数必须包含主键id，或者会返回位0
                if($wx->isUpdate()->save($data)){
                    unset($data['id']);
                    //更新缓存
                    $this->getSetting('wxconfig','Wxsetting',$data);
                    return ajaxMsg(1,'更新成功');
                }else{
                    return ajaxMsg(0,'更新失败');
                }
            }
        }
        //获取缓存
        $setting = $this->getSetting('wxconfig','Wxsetting');
        $this->assign('setting',$setting);
        return $this->fetch();
    }

}